package com.example.administrator.yuejian_2.activity;

<<<<<<< HEAD
=======

>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

<<<<<<< HEAD
import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.LoginBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.UserService;
=======
import com.example.administrator.yuejian_2.bean.LoginBean;
import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.LoginService;

>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

<<<<<<< HEAD
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText edit_name;
    private EditText edit_password;
    private Button login_button;
=======
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText edit_name;
    private EditText edit_password;
    private Button login_button;
    private Button reg_button;
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
<<<<<<< HEAD
        setContentView(R.layout.activity_login);

        initViews();//调用初始化方法
        initEvents();//调用事件方法

=======
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        initViews();//调用初始化方法
        initEvents();//调用事件方法


>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
    }

    public void initViews(){
        //初始化方法
        edit_name= (EditText) findViewById(R.id.edit_name);
        edit_password= (EditText) findViewById(R.id.edit_password);
        login_button= (Button) findViewById(R.id.login_button);
<<<<<<< HEAD
=======
        reg_button= (Button) findViewById(R.id.reg_button);
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
    }

    public void initEvents(){
        //事件（点击添加监听）
//        login_button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        login_button.setOnClickListener(this);
<<<<<<< HEAD

//
    }
=======
        reg_button.setOnClickListener(this);

//
    }


    @Override
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.login_button:
                //处理登录的点击事件
                if ("".equals(edit_name.getText().toString())||"".equals(edit_password.getText().toString()))
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
                else
                    login();
                break;
<<<<<<< HEAD
        }
    }
=======
            case R.id.reg_button:
                Intent intent = new Intent();//intent跳转
                finish();//关闭当前界面
                intent.setClass(LoginActivity.this, RegActivity.class);
                startActivity(intent);


        }
    }

>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
    public void login(){
        //建立与服务端的连接
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
<<<<<<< HEAD
        UserService userService = retrofit.create(UserService.class);
=======
        LoginService userService = retrofit.create(LoginService.class);
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
        //发送请求
        Call<LoginBean> call =
                userService.userLogin(edit_name.getText().toString(),edit_password.getText().toString());
        //处理服务器端的返回结果
        Callback<LoginBean> callback = new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
<<<<<<< HEAD
                int userid = response.body().getU_id();
=======
                String userid = response.body().getU_id();
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
                if ("0".equals(userid))
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                else {
                    saveUserInfo(userid);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();//intent跳转
                    finish();//关闭当前界面
                    intent.setClass(LoginActivity.this, MainActivity.class);//从当前界面跳转到MianActivity
                    startActivity(intent);


<<<<<<< HEAD


                    }
=======
                }
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2

            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                Toast.makeText(LoginActivity.this,"网络连接失败",Toast.LENGTH_LONG).show();

            }
        };
        call.enqueue(callback);//绑定请求与返回结果

    }



<<<<<<< HEAD
    public void saveUserInfo(int user_id){
=======
    public void saveUserInfo(String user_id){
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username", edit_name.getText().toString());
<<<<<<< HEAD
        int userid = user_id;
        editor.putInt("user_id", userid);
        editor.putString("password", edit_password.getText().toString());
=======
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("username", edit_password.getText().toString());
>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
        editor.commit();//提交修改


    }

<<<<<<< HEAD
}
=======



}

>>>>>>> b079ae49aea3652b48f9c84201c89333c2f216f2
